home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Devices / apps / ILBMtoRaw / ILBMtoRaw.c
Encoding:
C/C++ Source or Header  |  1998-04-26  |  4.6 KB  |  175 lines

  1. /*--------------------------------------------------------------*/
  2. /*                                */
  3. /* ILBMtoRaw: reads in ILBM, writes out raw file (raw planes,     */
  4. /*  followed by colormap)                     */
  5. /*                                                              */
  6. /* Based on ILBMRaw.c by Jerry Morrison and Steve Shaw,        */
  7. /* Electronic Arts.                               */
  8. /* Jan 31, 1986                            */
  9. /*                                                              */
  10. /* This software is in the public domain.                       */
  11. /* This version for the Amiga computer.                         */
  12. /*                                                              */
  13. /*  Callable from CLI ONLY                    */
  14. /*  modified 05-91 for use wuth iffparse modules        */
  15. /*  Requires linkage with several other modules - see Makefile  */
  16. /*--------------------------------------------------------------*/
  17.  
  18. #include "iffp/ilbmapp.h"
  19.  
  20. #ifdef LATTICE
  21. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  22. int chkabort(void) { return(0); }  /* really */
  23. #endif
  24.  
  25. char *vers = "\0$VER: ILBMtoRaw 37.5";
  26. char *Copyright = "ILBMtoRaw v37.5 (Freely Redistributable)";
  27.  
  28. void bye(UBYTE *s, int e);
  29. void cleanup(void);
  30.  
  31. LONG SaveBitMap(UBYTE *name, struct BitMap *bm, SHORT *cols, int ncols);
  32.  
  33. struct Library *IFFParseBase = NULL;
  34. struct Library *GfxBase = NULL;
  35.  
  36. /* ILBM frame */
  37. struct ILBMInfo ilbm = {0};
  38.  
  39.  
  40. /* ILBM Property chunks to be grabbed - BMHD and CMAP needed for this app
  41.  */
  42. LONG    ilbmprops[] = {
  43.         ID_ILBM, ID_BMHD,
  44.         ID_ILBM, ID_CMAP,
  45.         TAG_DONE
  46.         };
  47.  
  48. /* ILBM Collection chunks (more than one in file) to be gathered */
  49. LONG    *ilbmcollects = NULL;    /* none needed for this app */
  50.  
  51. /* ILBM Chunk to stop on */
  52. LONG    ilbmstops[] = {
  53.         ID_ILBM, ID_BODY,
  54.         TAG_DONE
  55.         };
  56.  
  57.  
  58. /** main() ******************************************************************/
  59.  
  60. void main(int argc, char **argv)
  61.     {
  62.     LONG error=NULL;
  63.     UBYTE *ilbmname, fname[80], buf[24];
  64.  
  65.     if ((argc < 2)||(argv[argc-1][0]=='?'))
  66.     bye("Usage from CLI: 'ILBMtoRaw filename'\n",RETURN_OK);
  67.     
  68.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  69.     bye("Can't open graphics.library",RETURN_FAIL);
  70.  
  71.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  72.     bye("Can't open iffparse.library",RETURN_FAIL);
  73.  
  74. /*
  75.  * Here we set up default ILBMInfo fields for our
  76.  * application's frames.
  77.  * Above we have defined the propery and collection chunks
  78.  * we are interested in (some required like BMHD)
  79.  */
  80.     ilbm.ParseInfo.propchks      = ilbmprops;
  81.     ilbm.ParseInfo.collectchks   = ilbmcollects;
  82.     ilbm.ParseInfo.stopchks      = ilbmstops;
  83.     if(!(ilbm.ParseInfo.iff = AllocIFF()))
  84.         bye(IFFerr(IFFERR_NOMEM),RETURN_FAIL);    /* Alloc an IFFHandle */
  85.  
  86.     ilbmname = argv[1];
  87.  
  88.     /* Load as a brush since we don't need to display it */
  89.     if (error = loadbrush(&ilbm,ilbmname))
  90.         {
  91.         printf("Can't load ilbm \"%s\", ifferr=%s\n",ilbmname,IFFerr(error));
  92.         bye("",RETURN_WARN);
  93.         }
  94.     else /* Successfully loaded ILBM */
  95.     {
  96.         strcpy(fname,argv[1]);
  97.  
  98.     if(ilbm.camg & HAM)    strcat(fname, ".ham");
  99.     if(ilbm.camg & EXTRA_HALFBRITE)    strcat(fname, ".ehb");
  100.  
  101.     if(ilbm.camg & HIRES)    strcat(fname, ".hi");
  102.     else strcat(fname, ".lo");
  103.  
  104.     if(ilbm.camg & LACE)    strcat(fname, ".lace");
  105.  
  106.     strcat(fname,".");
  107.     sprintf(buf,"%d",ilbm.Bmhd.w);
  108.     strcat(fname,buf);
  109.     strcat(fname,"x");
  110.     sprintf(buf,"%d",ilbm.Bmhd.h);
  111.     strcat(fname,buf);
  112.     strcat(fname,"x");
  113.     sprintf(buf,"%d",ilbm.brbitmap->Depth);
  114.     strcat(fname, buf);
  115.     printf(" Creating file %s \n", fname);
  116.     error=SaveBitMap(fname, ilbm.brbitmap, ilbm.colortable, ilbm.ncolors);
  117.  
  118.     unloadbrush(&ilbm);
  119.     }
  120.  
  121.     if(error)    bye(IFFerr(error),RETURN_WARN);
  122.     else    bye("",RETURN_OK);
  123.     }
  124.  
  125.  
  126. /* SaveBitMap (as raw planes and colortable)
  127.  *
  128.  * Given filename, bitmap structure, and colortable pointer,
  129.  * writes out raw bitplanes and colortable (not an ILBM)
  130.  * Returns 0 for success
  131.  */
  132.  
  133.  
  134. LONG SaveBitMap(UBYTE *name, struct BitMap *bm, SHORT *cols, int ncols)
  135.     {
  136.     SHORT i;
  137.     LONG nb,plsize;
  138.  
  139.     LONG file = Open( name, MODE_NEWFILE);
  140.     if( file == 0 )
  141.     {
  142.     printf(" couldn't open %s \n",name);
  143.     return(CLIENT_ERROR);    /* couldnt open a load-file */    
  144.     }
  145.     plsize = bm->BytesPerRow*bm->Rows;
  146.     for (i=0; i<bm->Depth; i++)
  147.     {
  148.     nb =  Write(file, bm->Planes[i], plsize);
  149.     if (nb<plsize) break;
  150.     }
  151.     if(nb>0)    nb=Write(file, cols, (1<<bm->Depth)*2);    /* save color map */
  152.     Close(file);
  153.     return(nb >= 0 ? 0L : IFFERR_WRITE);
  154.     }
  155.  
  156. void bye(UBYTE *s, int e)
  157.     {
  158.     if(s&&(*s))    printf("%s\n",s);
  159.     cleanup();
  160.     exit(e);
  161.     }
  162.  
  163. void cleanup()
  164.     {
  165.     if(ilbm.ParseInfo.iff)        FreeIFF(ilbm.ParseInfo.iff);
  166.  
  167.     if(IFFParseBase)    CloseLibrary(IFFParseBase);
  168.     if(GfxBase)        CloseLibrary(GfxBase);
  169.     }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.